﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.geom.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import GameMode.*;
    import Engine.*;
    import com.zynga.skelly.util.*;

    public class HarvestableResource extends MechanicMapResource implements IPeepWalker {

        public static const STATE_PLOWED:String = "plowed";
        public static const STATE_PLANTED:String = "planted";
        public static const STATE_GROWN:String = "grown";
        public static const STATE_FALLOW:String = "fallow";
        public static const STATE_WITHERED:String = "withered";
        public static const IS_PLANTING:String = "planting";
        public static const IS_HARVESTING:String = "harvesting";
        public static const IS_PLOWING:String = "plowing";
        public static const IS_RESTING:String = "";
        protected static const GROW_UPDATE_RATE:Number = 1;
        public static const WORKER_BUCKET_PREFIX:String = "w";

        protected var m_buildTime:Number;
        protected var m_plantTime:Number;
        protected var m_timeSinceGrowUpdate:Number = 0;
        protected var m_growPercentage:Number = 0;
        protected var m_witherPercentage:Number = 0;
        protected var m_buildPowerups:int = 0;
        protected var m_harvestCounter:int = 0;
        protected var m_heldEnergy:Number = 0;
        protected var m_growTimeLeft:int;
        protected var m_contract:Item;
        protected var m_contractGrowTime:Number;
        protected var m_witherOn:Boolean;
        protected var m_finishing:Boolean = false;
        protected var m_isAccelerating:Boolean = false;
        protected var m_isHarvesting:Boolean = false;
        protected var m_accelerateStartTime:Number = 0;
        protected var m_growTimeLeftAtAccelStart:Number = 0;
        protected var m_lastVisitorHelpType:String = "";
        protected var m_firstHarvestAttempt:Boolean = true;
        protected var m_commodities:Array;

        public function HarvestableResource(_arg1:String){
            super(_arg1);
            setState(STATE_PLOWED);
            this.plantTime = GlobalEngine.getTimer();
            this.m_buildTime = GlobalEngine.getTimer();
        }
        override public function getSaveObject():Object{
            var _local1:Object = super.getSaveObject();
            _local1.plantTime = this.plantTime;
            _local1.buildTime = this.m_buildTime;
            return (_local1);
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.plantTime = _arg1.plantTime;
            this.m_buildTime = _arg1.buildTime;
            this.harvestCounter = _arg1.harvestCounter;
            this.m_buildPowerups = _arg1.buildPowerups;
            this.m_witherOn = _arg1.witherOn;
            m_dbgServerConnectsToRoad = (_arg1.isConnectedToRoad == true);
            this.updatePercentages();
        }
        public function get isWitherOn():Boolean{
            return (this.m_witherOn);
        }
        override public function isSellable():Boolean{
            return (true);
        }
        override protected function computeDefaultHotspots():Vector.<Vector3>{
            var _local1:Vector.<Vector3> = new Vector.<Vector3>(1);
            _local1[0] = new Vector3(0, 0);
            return (_local1);
        }
        public function getGrowTimeDelta():Number{
            var _local1:Number = (Global.gameSettings.inGameDaySeconds * 1000);
            var _local2:Number = harvestingDefinition.growTime;
            return (((_local2 * _local1) * Global.gameSettings.growMultiplier));
        }
        protected function getWitherTimeDelta():Number{
            var _local1:Number = (Global.gameSettings.inGameDaySeconds * 1000);
            var _local2:Number = harvestingDefinition.growTime;
            return (((_local2 * _local1) * Global.gameSettings.witherMultiplier));
        }
        public function getGrowTimeLeft():int{
            return (this.m_growTimeLeft);
        }
        public function isHarvestable():Boolean{
            return ((m_state == STATE_GROWN));
        }
        public function isPlantable(_arg1:String):Boolean{
            return ((((m_state == STATE_PLOWED)) && (hasValidId())));
        }
        public function isPlanted():Boolean{
            return ((m_state == STATE_PLANTED));
        }
        public function isPlowable():Boolean{
            return ((((m_state == STATE_FALLOW)) || ((m_state == STATE_WITHERED))));
        }
        public function isWithered():Boolean{
            return ((m_state == STATE_WITHERED));
        }
        public function isGrown():Boolean{
            return ((m_state == STATE_GROWN));
        }
        public function get heldEnergy():Number{
            return (this.m_heldEnergy);
        }
        public function set heldEnergy(_arg1:Number):void{
            this.m_heldEnergy = _arg1;
        }
        public function boostGrowth(_arg1:Number=2):void{
            var _local2:Number = _arg1;
            this.m_plantTime = (this.m_plantTime - _local2);
        }
        public function setFullGrown():void{
            this.m_plantTime = (GlobalEngine.getTimer() - this.getGrowTimeDelta());
            this.m_growPercentage = 100;
            this.m_witherPercentage = 0;
            setState(STATE_GROWN);
        }
        override public function onUpdate(_arg1:Number):void{
            var _local4:Boolean;
            if (_arg1){
                this.m_timeSinceGrowUpdate = (this.m_timeSinceGrowUpdate + _arg1);
            };
            var _local2 = (this.m_timeSinceGrowUpdate >= HarvestableResource.GROW_UPDATE_RATE);
            if (((_local2) || (this.m_isAccelerating))){
                this.updatePercentages();
                this.m_timeSinceGrowUpdate = 0;
            };
            var _local3:Boolean = this.updateState(_arg1);
            if (((!(_local3)) && (_local2))){
                _local4 = this.updateStatePhase();
                if (_local4){
                    reloadImage();
                };
            };
            super.onUpdate(_arg1);
        }
        protected function updateState(_arg1:Number):Boolean{
            var _local2:Boolean;
            if ((((m_state == STATE_PLANTED)) && ((this.getGrowPercentage() == 100)))){
                setState(STATE_GROWN);
            } else {
                if ((((m_state == STATE_GROWN)) && ((this.getWitherPercentage() == 100)))){
                    setState(STATE_WITHERED);
                } else {
                    _local2 = false;
                };
            };
            return (_local2);
        }
        override protected function onStateChanged(_arg1:String, _arg2:String):void{
            super.onStateChanged(_arg1, _arg2);
            if (_arg1 != _arg2){
                this.updateStatePhase();
            };
        }
        protected function updateStatePhase():Boolean{
            var _local1:String = this.computeStatePhase();
            if (_local1 != m_statePhase){
                m_statePhase = _local1;
                return (true);
            };
            return (false);
        }
        protected function computeStatePhase():String{
            return (Item.DEFAULT_PHASE);
        }
        override public function onPlayAction():void{
            if (m_visitReplayLock > 0){
                return;
            };
            super.onPlayAction();
        }
        public function plow():Boolean{
            return (false);
        }
        public function plant(_arg1:String):Boolean{
            return (false);
        }
        public function harvest():Boolean{
            return (false);
        }
        protected function onVisitorHelp(_arg1:String):void{
            var _local2:int;
            var _local3:int;
            var _local4:int;
            if (Global.isVisiting()){
                _local2 = friendVisitRepGain();
                _local3 = Global.gameSettings.getInt("friendHelpDefaultGoodsReward", 1);
                _local4 = Global.gameSettings.getInt("friendHelpDefaultCoinReward", 1);
                switch (_arg1){
                    case STATE_GROWN:
                        finalizeAndAwardVisitorHelp(this.m_lastVisitorHelpType, _local2, 0, _local3);
                        break;
                    default:
                        finalizeAndAwardVisitorHelp(this.m_lastVisitorHelpType, _local2, _local4, 0);
                };
            };
        }
        public function visitBoost():Boolean{
            var _local1:Boolean = Global.isVisiting();
            this.setVisitorInteractedHighlighted(_local1);
            var _local2:Number = this.getVisitBoost();
            var _local3:Number = this.getInstantLimit();
            var _local4:Number = this.getGrowTimeDelta();
            if (((((this.m_growTimeLeft * 1000) < _local2)) || ((_local4 <= _local3)))){
                this.setFullGrown();
            } else {
                this.boostGrowth(_local2);
            };
            this.onVisitorHelp(STATE_PLANTED);
            return (true);
        }
        protected function getInstantLimit():Number{
            var _local1:Number = (Global.gameSettings.dailyCollectCycleTimeSeconds * 1000);
            var _local2:Number = Global.gameSettings.boostGrowInstantHourLimit;
            if (_local2 != 0){
                _local2 = (_local2 / 23);
            };
            return (((_local2 * _local1) * Global.gameSettings.growMultiplier));
        }
        protected function getVisitBoost():Number{
            var _local1:Number = (Global.gameSettings.inGameDaySeconds * 1000);
            var _local2:Number = harvestingDefinition.growTime;
            return ((((_local2 * _local1) * Global.gameSettings.growMultiplier) * Global.gameSettings.boostGrowMultiplier));
        }
        public function revive():Boolean{
            this.setFullGrown();
            this.onVisitorHelp(STATE_WITHERED);
            return (true);
        }
        public function getHarvestTime():Number{
            return (Global.gameSettings.getNumber("actionBarHarvest"));
        }
        public function get plantTime():Number{
            return (this.m_plantTime);
        }
        public function set plantTime(_arg1:Number):void{
            this.m_plantTime = _arg1;
            this.m_growPercentage = 0;
            this.m_witherPercentage = 0;
        }
        public function get harvestCounter():int{
            return (this.m_harvestCounter);
        }
        public function set harvestCounter(_arg1:int):void{
            this.m_harvestCounter = _arg1;
        }
        public function doHarvestDropOff(_arg1:Boolean=true):Object{
            Sounds.playFromSet(Sounds.SET_MONEY);
            return (null);
        }
        override public function getActionText():String{
            switch (m_state){
                case STATE_WITHERED:
                    if (Global.isVisiting()){
                        return (this.getRevivingText());
                    };
                    return (this.getClearingText());
                case STATE_PLANTED:
                    if (Global.isVisiting()){
                        return (this.getWateringText());
                    };
                    return (super.getActionText());
                case STATE_GROWN:
                    return (this.getHarvestingText());
                default:
                    return (super.getActionText());
            };
        }
        public function getFinishingText():String{
            return (ZLoc.t("Main", "Finishing"));
        }
        public function getHarvestingText():String{
            return (ZLoc.t("Main", "Harvesting"));
        }
        public function getWateringText():String{
            return (ZLoc.t("Main", "Watering"));
        }
        public function getRevivingText():String{
            return (ZLoc.t("Main", "Reviving"));
        }
        public function getClearingText():String{
            return (ZLoc.t("Main", "Clearing"));
        }
        protected function updatePercentages():void{
            var _local1:Number;
            var _local2:Number;
            var _local3:Number;
            var _local4:Number;
            var _local5:Number;
            var _local6:Number;
            this.m_growPercentage = 0;
            this.m_witherPercentage = 0;
            if (this.plantTime > 0){
                _local1 = GlobalEngine.getTimer();
                _local2 = (_local1 - this.plantTime);
                _local3 = this.getGrowTimeDelta();
                if (_local2 < _local3){
                    _local4 = (_local2 / _local3);
                    this.m_growPercentage = Math.floor(MathUtil.clamp((_local4 * 100), 0, 100));
                    if (((((this.m_isAccelerating) && ((this.m_accelerateStartTime > 0)))) && ((this.m_growTimeLeftAtAccelStart > 0)))){
                        this.m_growTimeLeft = this.calculateAcceleratedTimeLeft(_local1);
                    } else {
                        this.m_growTimeLeft = ((_local3 - _local2) / 1000);
                    };
                } else {
                    this.m_growPercentage = 100;
                    if (this.isWitherOn){
                        _local5 = this.getWitherTimeDelta();
                        if (_local2 < _local5){
                            _local6 = (_local2 - _local3);
                            _local4 = (_local6 / (_local5 - _local3));
                            this.m_witherPercentage = Math.floor(MathUtil.clamp((_local4 * 100), 0, 100));
                        } else {
                            this.m_witherPercentage = 100;
                        };
                    };
                };
            };
        }
        protected function calculateAcceleratedTimeLeft(_arg1:Number):int{
            var _local2:Number = (this.m_accelerateStartTime + (this.getHarvestTime() * 1000));
            var _local3:int = ((((_local2 - _arg1) / (_local2 - this.m_accelerateStartTime)) * this.m_growTimeLeftAtAccelStart) / 1000);
            if (_local3 < 0){
                _local3 = 0;
            };
            return (_local3);
        }
        public function getGrowPercentage():Number{
            return (this.m_growPercentage);
        }
        public function getWitherPercentage():Number{
            return (this.m_witherPercentage);
        }
        public function getBuildingSpeedUpCost():Number{
            return (Math.max(1, (getItem().buildEnergyCost * Global.gameSettings.buildingSpeedupCostMultiplier)));
        }
        public function onHarvestComplete(_arg1:Object):void{
            this.parseAndCheckDooberResults(_arg1);
        }
        override protected function getSellPrice():int{
            return (m_item.sellPrice);
        }
        protected function getNextActor(_arg1:MapResource):NPC{
            var _local5:NPC;
            var _local7:Number;
            var _local2:Array = Global.world.citySim.npcManager.getFreeAgentWalkers(this);
            var _local3:NPC;
            var _local4:Number = Number.POSITIVE_INFINITY;
            for each (_local5 in _local2) {
                if (_local5.isFreeAgent){
                    _local7 = _local5.getPosition().subtract(this.getPosition()).length();
                    if (_local7 < _local4){
                        _local4 = _local7;
                        _local3 = _local5;
                    };
                };
            };
            if (_local3 != null){
                return (_local3);
            };
            var _local6:NPC = Global.world.citySim.npcManager.createWalker(_arg1, false);
            return (_local6);
        }
        override public function getCursor():Class{
            var _local1:Class = super.getCursor();
            switch (m_state){
                case STATE_GROWN:
                    _local1 = this.getGrownCursor();
                    break;
                case STATE_PLANTED:
                    _local1 = this.getPlantedCursor();
                    break;
                case STATE_PLOWED:
                    _local1 = this.getPlowedCursor();
                    break;
                case STATE_WITHERED:
                    _local1 = this.getWitheredCursor();
                    break;
            };
            return (_local1);
        }
        protected function getGrownCursor():Class{
            return (null);
        }
        protected function getPlantedCursor():Class{
            return (null);
        }
        protected function getPlowedCursor():Class{
            return (null);
        }
        protected function getWitheredCursor():Class{
            return (null);
        }
        protected function getFallowToolTipStatus():String{
            return (ZLoc.t("Main", "FallowPlot"));
        }
        protected function getPlowedToolTipStatus():String{
            return (ZLoc.t("Main", "PlowedPlot"));
        }
        protected function getPlantedToolTipStatus():String{
            var _local2:String;
            var _local1:String;
            if (((((this.m_isAccelerating) && ((this.m_accelerateStartTime > 0)))) && ((this.m_growTimeLeftAtAccelStart > 0)))){
                _local1 = ZLoc.t("Main", "BuildingAccelerating");
            } else {
                _local2 = GameUtil.formatMinutesSeconds(this.getGrowTimeLeft());
                _local1 = this.getTimeLeftString(_local2);
            };
            return (_local1);
        }
        protected function getTimeLeftString(_arg1:String):String{
            return (ZLoc.t("Main", "PlantedPlot", {time:_arg1}));
        }
        protected function getGrownToolTipStatus():String{
            return (ZLoc.t("Main", "GrownPlot"));
        }
        protected function getWitheredToolTipStatus():String{
            var _local1:String;
            var _local2:int = Global.gameSettings.getInt("plowCost");
            if (Global.player.canBuy(_local2, false)){
                _local1 = ZLoc.t("Main", "WitheredPlot");
            } else {
                _local1 = ZLoc.t("Main", "WitheredPlot_NoMoney");
            };
            return (_local1);
        }
        override public function getToolTipStatus():String{
            var _local1:String;
            if (((!(Global.isVisiting())) && (!(Global.world.isEditMode)))){
                switch (m_state){
                    case STATE_FALLOW:
                        _local1 = this.getFallowToolTipStatus();
                        break;
                    case STATE_PLOWED:
                        _local1 = this.getPlowedToolTipStatus();
                        break;
                    case STATE_PLANTED:
                        _local1 = this.getPlantedToolTipStatus();
                        break;
                    case STATE_GROWN:
                        _local1 = this.getGrownToolTipStatus();
                        break;
                    case STATE_WITHERED:
                        _local1 = this.getWitheredToolTipStatus();
                        break;
                };
            };
            return (_local1);
        }
        override public function getToolTipAction():String{
            var _local2:int;
            var _local1:String = super.getToolTipAction();
            if (((((!(_local1)) && (!(Global.isVisiting())))) && (!(Global.world.isEditMode)))){
                switch (m_state){
                    case STATE_FALLOW:
                        _local1 = ZLoc.t("Main", "FallowPlotAction");
                        break;
                    case STATE_PLOWED:
                        _local1 = ZLoc.t("Main", "PlowedPlotAction");
                        break;
                    case STATE_GROWN:
                        this.getGrownToolTipAction();
                        break;
                    case STATE_WITHERED:
                        _local2 = Global.gameSettings.getInt("plowCost");
                        if (Global.player.canBuy(_local2, false)){
                            _local1 = ZLoc.t("Main", "WitheredPlotAction");
                        } else {
                            _local1 = ZLoc.t("Main", "WitheredPlot_NoMoney_Action");
                        };
                        break;
                };
            };
            return (_local1);
        }
        protected function getGrownToolTipAction():String{
            return (ZLoc.t("Main", "GrownAction"));
        }
        protected function getContractFriendlyName():String{
            if (!this.m_contract){
                return (null);
            };
            return (ZLoc.t("Items", (this.m_contract.name + "_friendlyName")));
        }
        public function getHarvestingDefinition():Item{
            if (!this.m_contract){
                return (m_item);
            };
            return (this.m_contract);
        }
        override public function getProgressBarEndFunction():Function{
            if (Global.isVisiting()){
                switch (m_state){
                    case STATE_PLANTED:
                        return (this.visitBoost);
                    case STATE_WITHERED:
                        return (this.revive);
                };
            } else {
                switch (m_state){
                    case STATE_PLANTED:
                        if ((((m_actionMode == VISIT_PLAY_ACTION)) || ((m_actionMode == VISIT_REPLAY_ACTION)))){
                            return (this.visitBoost);
                        };
                        return (null);
                    case STATE_WITHERED:
                        if ((((m_actionMode == VISIT_PLAY_ACTION)) || ((m_actionMode == VISIT_REPLAY_ACTION)))){
                            return (this.revive);
                        };
                        return (this.plow);
                };
            };
            return (Curry.curry(function (_arg1:HarvestableResource):void{
                if (_arg1.harvest()){
                    _arg1.doHarvestDropOff();
                };
            }, this));
        }
        override public function getActionTargetName():String{
            var _local1 = "";
            if (harvestingDefinition){
                _local1 = harvestingDefinition.name;
            };
            return (_local1);
        }
        override public function addSupply(_arg1:String, _arg2:int):Boolean{
            var _local5:String;
            var _local6:Boolean;
            var _local7:Boolean;
            var _local3:Boolean;
            if (!this.acceptsAsSupplies(_arg1)){
                _arg1 = this.getFallbackCommodity(_arg1, harvestingDefinition.commodityReq);
            };
            if (!_arg1){
                return (false);
            };
            var _local4:int = (harvestingDefinition.commodityReq - Global.player.commodities.getCount(_arg1));
            if (_local4 > 0){
                _local5 = this.getFallbackCommodity(_arg1, _local4);
                if (_local5){
                    _local6 = true;
                    _local7 = true;
                    if (Global.player.commodities.getCount(_arg1) > 0){
                        _local6 = this.addSupplyHelper(_arg1, Global.player.commodities.getCount(_arg1));
                    };
                    if (_local4 > 0){
                        _local7 = this.addSupplyHelper(_local5, _local4);
                    };
                    _local3 = ((_local6) && (_local7));
                    if (!_local3){
                        clearSupply();
                    };
                };
            } else {
                _local3 = this.addSupplyHelper(_arg1, _arg2);
            };
            return (_local3);
        }
        protected function addSupplyHelper(_arg1:String, _arg2:int):Boolean{
            doResourceChanges(0, 0, 0, -(_arg2), _arg1, true);
            var _local3:Boolean = Global.player.commodities.remove(_arg1, _arg2);
            this.doSupplyDoobers(_arg1, _arg2);
            if (_local3){
                _local3 = super.addSupply(_arg1, _arg2);
            };
            return (_local3);
        }
        public function doSupplyDoobers(_arg1:String, _arg2:Number):void{
            var _local3:Point = Global.hud.localToGlobal(Global.hud.hudGoodsPosition);
            var _local4:String = Global.gameSettings.getDooberFromType(_arg1, _arg2);
            var _local5:Point = IsoMath.viewportToStage(GMObjectMove.getPixelCenter(this));
            IsoMath.viewportToStage(GMObjectMove.getPixelCenter(this)).y = (_local5.y - 40);
            var _local6:int = (int((_arg2 / 20)) + 1);
            while (_local6 > 1) {
                setTimeout(Global.world.dooberManager.createDummyDoober, 200, _local4, _local3, _local5);
                _local6--;
            };
            if (_local6 == 1){
                Global.world.dooberManager.createDummyDoober(_local4, _local3, _local5);
            };
        }
        public function getWorkerBucket():String{
            return ((WORKER_BUCKET_PREFIX + this.getId()));
        }
        public function get commodities():Array{
            var _local1:Object;
            if (!this.m_commodities){
                this.m_commodities = new Array();
                for each (_local1 in m_item.commodities) {
                    this.m_commodities.push(_local1.name);
                };
            };
            return (this.m_commodities);
        }
        override protected function getSupplyCapacities():Object{
            var _local2:String;
            var _local1:Object = new Object();
            for each (_local2 in this.commodities) {
                _local1[_local2] = harvestingDefinition.commodityReq;
            };
            return (_local1);
        }
        public function acceptsAsSupplies(_arg1:String):Boolean{
            var _local2:String;
            for each (_local2 in this.commodities) {
                if (_arg1 == _local2){
                    return (true);
                };
            };
            return (false);
        }
        protected function getFallbackCommodity(_arg1:String, _arg2:int):String{
            var _local4:String;
            var _local3:String = Global.gameSettings.getCommodityXMLDefault().@name;
            if (((this.acceptsAsSupplies(_local3)) && ((Global.player.commodities.getCount(_local3) >= _arg2)))){
                return (_local3);
            };
            for each (_local4 in this.commodities) {
                if (((((!((_local4 == _local3))) && (!((_local4 == _arg1))))) && ((Global.player.commodities.getCount(_local4) >= _arg2)))){
                    return (_local4);
                };
            };
            return (null);
        }
        public function getSupplyBonus():Number{
            var _local3:String;
            var _local4:Number;
            var _local1:int;
            var _local2:Number = 0;
            for each (_local3 in this.commodities) {
                _local4 = (1 + (Global.gameSettings.getCommodityBonus(_local3) / 100));
                _local1 = (_local1 + getSupply(_local3));
                _local2 = (_local2 + (getSupply(_local3) * _local4));
            };
            if (_local1 > 0){
                _local2 = (_local2 / _local1);
            } else {
                _local2 = 1;
            };
            return (_local2);
        }
        public function getMapResource():MapResource{
            return ((this as MapResource));
        }

    }
}//package Classes 
